<template>
  <div class="prj-edit-information-view">

    <el-form ref="ruleForm" :inline="true" :rules="rules" :model="formData" class="demo-form-inline" label-width="120px" size="medium"
             :disabled="formData.status > 0">

      <el-form-item label="项目所属阶段" prop="project_phases_id">
        <el-select v-model="formData.project_phases_id" placeholder="请选择项目所属阶段" clearable @change="phasesChange">
          <el-option
            v-for="item in phasesOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="关联项目" v-if="formData.project_phases_id == 2 && !formData.status">
        <el-select v-model="formData.parent_id" placeholder="请选择关联项目" clearable @change="projectChange">
          <el-option
            v-for="item in projectOptions"
            :key="item.id"
            :label="item.project_name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="立项信息" v-if="formData.status || !formData.parent_id">
        <el-select v-model="formData.demand_info_id" placeholder="请选择立项信息" clearable @change="initChange" v-if="!formData.status">
          <el-option
            v-for="item in initList"
            :key="item.info_id"
            :label="item.project_name"
            :value="item.info_id">
          </el-option>
        </el-select>
        <el-select v-model="formData.demand_info_name" placeholder="请选择立项信息" clearable @change="initChange" v-else>
          <el-option
            v-for="item in initList"
            :key="item.info_id"
            :label="item.project_name"
            :value="item.info_id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目来源" prop="project_source_id">
        <el-select v-model="formData.project_source_id" placeholder="请选择项目来源" clearable @change="sourceChange">
          <el-option
            v-for="item in projectSourceOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目编号" prop="project_no">
        <el-input v-model="formData.project_no" :disabled="isNoDisable" placeholder="请输入项目编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="项目名称" prop="project_name">
        <el-input v-model="formData.project_name" placeholder="请输入项目名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="项目级别" prop="project_level_id">
        <el-select v-model="formData.project_level_id" placeholder="请选择项目级别" clearable @change="levelChange">
          <el-option
            v-for="(item,index) in options"
            :key="index"
            :label="item.dict_val"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="项目类型" prop="project_category_id">
        <el-select v-model="formData.project_category_id" placeholder="请选择项目类型" clearable @change="categoryChange">
          <el-option
            v-for="(item,index) in projectTypeOptions"
            :key="index"
            :label="item.dict_val"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="申报类别" prop="sign_category_id">
        <el-select v-model="formData.sign_category_id" placeholder="请选择申报类别" clearable @change="signChange">
          <el-option
            v-for="(item,index) in applyTypeOptions"
            :key="index"
            :label="item.dict_val"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="药物成分">
        <el-input v-model="formData.drug_ingredient" placeholder="请输入药物成分" clearable></el-input>
      </el-form-item>
      <el-form-item label="药物类别">
        <el-input v-model="formData.drug_category" placeholder="请输入药物类别" clearable></el-input>
      </el-form-item>
      <el-form-item label="功能主治">
        <el-input v-model="formData.function_attend" placeholder="请输入功能主治" clearable></el-input>
      </el-form-item>
      <el-form-item label="剂型与规格">
        <el-input v-model="formData.drug_or_specifications" placeholder="请输入剂型与规格" clearable></el-input>
      </el-form-item>
      <el-form-item label="委托单位" v-if="formData.project_source_id == 2">
        <el-input v-model="formData.entrusting_party" placeholder="请输入委托单位"></el-input>
      </el-form-item>
<!--      <el-form-item label="项目负责人" prop="project_person_in_charge_id">-->
<!--        <el-select v-model="formData.project_person_in_charge_id" @change="peopleChange">-->
<!--          <el-option-->
<!--            v-for="item in peoples"-->
<!--            :key="item.id"-->
<!--            :label="item.name"-->
<!--            :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="项目负责人" prop="value">
<!--        <el-cascader v-model="formData.value"-->
<!--                     :show-all-levels="false"-->
<!--                     :options="departments"-->
<!--                     :props="props"-->
<!--                     :placeholder="formData.project_person_in_charge"-->
<!--                     :change-on-select="false"-->
<!--                     filterable-->
<!--                     @change="cascaderChange">-->
<!--          <template slot-scope="{ node, data }">-->
<!--            <span v-if="node.level == 1">{{ data.department_name }}</span>-->
<!--            <span v-else>{{ data.user_name }}</span>-->
<!--          </template>-->
<!--        </el-cascader>-->

        <el-cascader
          clearable
          :show-all-levels="false"
          v-model="formData.value"
          :placeholder="formData.project_person_in_charge?formData.project_person_in_charge:'请选择项目负责人'"
          :options="options11"
          @change="handleChange"
          filterable></el-cascader>
      </el-form-item>




    </el-form>

  </div>
</template>

<script>
    export default {
        name: "informationView",
        props:{

        },
        data:function () {
            return{
                value:'',
                formData:{
                    project_no:'',                    //项目编号
                    project_name:'',                  //项目名称
                    demand_info_id:'',                //立项id
                    demand_info_name:'',              //立项名称
                    project_source_id:'',             //项目来源id
                    project_source_name:'',             //项目来源
                    project_phases_id:'',             //项目阶段id
                    project_phases_name:'',           //项目阶段
                    project_level_id:'',              //项目级别id
                    project_level_name:'',            //项目级别名称
                    project_category_id:'',           //项目类别id
                    project_category_name:'',         //项目类型
                    sign_category_id:'',              //申报类别id
                    sign_category_name:'',            //申报类别
                    drug_category:'',                 //药物类别
                    drug_ingredient:'',               //药物成分
                    function_attend:'',               //功能主治
                    drug_or_specifications:'',        //剂型与规格
                    project_template_id:'',           //项目模板id
                    project_template_name:'',         //项目模板名称
                    project_person_in_charge_id:'',   //项目负责人id
                    projectPersonInCharge:'',      //项目负责人
                    project_duration:'',              //项目工期
                    plan_start_time:'',               //开始时间
                    plan_end_time:'',                 //结束时间
                    entrusting_party:'',              //委托单位
                    project_design_book:{},
                    member:[],
                    project_task:[],
                    value:[],
                    parent_id:'',                     //父id
                },
                options: [],
                initList:[],
                projectOptions:[],
                projectSourceOptions: [
                    {
                        label: "自研",
                        value: "1",
                    },
                    {
                        label: "委托",
                        value: "2",
                    },
                ],
                projectTypeOptions: [],
                applyTypeOptions: [],
                phasesOptions: [
                    {
                        label: "临床前研究",
                        value: "1",
                    },
                    {
                        label: "临床研究",
                        value: "2",
                    },
                ],
                peoples:[],
                rules: {
                    project_no: [
                        { required: true, message: '请输入项目编号', trigger: 'blur' }
                    ],
                    project_name: [
                        { required: true, message: '请输入项目名称', trigger: 'blur' }
                    ],
                    project_source_id: [
                        { required: true, message: '请选择项目来源', trigger: 'change' }
                    ],
                    project_phases_id: [
                        { required: true, message: '请选择项目所属阶段', trigger: 'change' }
                    ],
                    project_level_id: [
                        { required: true, message: '请选择项目级别', trigger: 'change' }
                    ],
                    project_category_id: [
                        { required: true, message: '请选择项目类型', trigger: 'change' }
                    ],
                    sign_category_id: [
                        { required: true, message: '请选择申报类别', trigger: 'change' }
                    ],
                    value: [
                        { required: true, message: '请选择项目负责人', trigger: 'change' }
                    ]
                },
                departments:[],
                props: {
                    value:'id',
                    label:'department_name',
                    lazy: true,
                    lazyLoad : ((node, resolve) => {

                        if (node.level == 1){
                            if (node.data){
                                let d = {
                                    department_id:node.data.id,
                                    limit:1000,
                                    skip:1,
                                    user_status:1,
                                }

                                this.$api.GET_USER_LIST(d).then(res=>{
                                    if (res.code == 200){

                                        for (let i = 0; i < res.data.data.length; i++){
                                            res.data.data[i].leaf = true;
                                            res.data.data[i].department_name = res.data.data[i].user_name
                                        }
                                        this.departmentMembers = res.data.data;
                                        resolve(res.data.data);
                                    }
                                })
                            }
                        }
                    })
                },
                departmentMembers:[],

                options11:[],

                isNoDisable:false,
            }
        },
        created:function(){
            this.$api.USER_GROUP_DEPARTMENT_LIST().then(res=>{
                if (res.code == 200){
                    res.data.data.forEach(mem=>{
                        mem.label = mem.name
                        mem.value = mem.id
                        if (!mem.children || !mem.children.length){
                            mem.leaf = true
                        }else if (mem.children.length) {
                            mem.children.forEach(item=>{
                                item.leaf = true;
                                item.value = item.id;
                                item.label = item.name;
                                item.department_id = item.id;
                            })
                        }
                    })
                    this.options11 = res.data.data;
                }else{
                    this.$message.error(res.msg)
                }
            });
        },
        mounted() {
            this.getProjectInitList();
            this.getProjectList()

            if (this.$store.getters.sysDic()){
                this.options = this.$store.getters.sysDic('project_level')
                this.projectTypeOptions = this.$store.getters.sysDic('project_type')
                this.applyTypeOptions = this.$store.getters.sysDic('apply_type')
            }else {
                this.$store.dispatch("sys/getSysAllDic").then(data=>{
                    this.options = data['project_level']
                    this.projectTypeOptions = data['project_type']
                    this.applyTypeOptions = data['apply_type']
                })
            }
            console.log(this.$store.getters.sysDic())

            this.departments = [...this.$store.getters.department]
            this.departments.forEach(item=>{
                item.disabled = false
            })
        },
        methods:{
            handleChange:function(value){
                console.log(value)

                if (value && value[1]){
                    let index = this.options11.findIndex(a=>a.id == value[0])
                    if (index != -1){
                        let dep = this.options11[index]
                        let index1 = dep.children.findIndex(a=>a.id == value[1])
                        if (index1 != -1){
                            this.formData.project_person_in_charge_id = dep.children[index1].id
                            this.formData.project_person_in_charge = dep.children[index1].name
                        }
                    }
                }else {
                    this.formData.project_person_in_charge_id = ''
                    this.formData.project_person_in_charge = ''
                }
            },
            cascaderChange:function(node){
                if(node && node[1]){
                    let a = this.departmentMembers.findIndex(a=>a.id == node[1])
                    if (a != -1){
                        //this.value = this.value + "/" + this.departmentMembers[a].user_name
                        this.formData.project_person_in_charge_id = this.departmentMembers[a].id
                        this.formData.project_person_in_charge = this.departmentMembers[a].user_name
                    }
                }

            },
            setData:function(data){
                this.formData = data;
                this.formData.value = data.project_person_in_charge_id ? [data.project_person_in_charge_id] : ''

            },
            getProjectList:function(){
                this.$api.PROJECT_START_PROJECT_BEFORE_LIST().then((res)=>{
                    if (res.code == 200){
                        if (res.data && res.data.data){
                            this.projectOptions = res.data.data || []
                            console.log(this.projectOptions)
                        }

                    }else {
                        console.log(res.msg)
                    }
                }).catch(err=>{

                })
            },
            getProjectInitList:function () {
                this.$api.PROJECT_INIT_AGREE_LIST().then(res=>{
                    if (res.code == 200){
                        this.initList = res.data;
                    }else {
                        this.$message.error(res.msg);
                    }
                }).catch(err=>{

                })
            },
            initChange:function (id) {
                console.log(id)
                if (id){
                    let index = this.initList.findIndex(a=>a.info_id == id)
                    if (index != -1){
                        let val = this.initList[index]


                        this.$api.PROJECT_INIT_AGREE_DETAIL(val.info_id).then(res=>{
                            if (res.code == 200){

                                res.data.project_phases_name = this.formData.project_phases_name
                                res.data.project_phases_id = this.formData.project_phases_id
                                // this.formData = JSON.parse(JSON.stringify(res.data));

                                this.formData.id = null


                                this.formData.project_no = res.data.project_no
                                this.formData.project_name = res.data.project_name
                                // this.formData.project_level_id = res.data.project_level_id
                                this.formData.project_category_id = res.data.project_type_id
                                this.formData.project_category_name = res.data.project_type
                                this.formData.project_source_id = res.data.project_source_id
                                this.formData.project_source_name = res.data.project_source
                                this.formData.sign_category_id = res.data.apply_type_id
                                this.formData.sign_category_name = res.data.apply_type
                                this.formData.drug_category = res.data.medical_type
                                this.formData.drug_or_specifications = res.data.dosage_norms
                                this.formData.drug_ingredient = res.data.medical_ingredient
                                this.formData.function_attend = res.data.function_attending
                                this.formData.demand_info_id = val.info_id
                                this.formData.demand_info_name = val.project_name;
                                // this.formData.project_source_name = this.formData.project_source


                                // this.$set(this.formData,this.formData.project_category_id,res.data.project_type_id)
                                // this.$set(this.formData,this.formData.project_category_name,res.data.project_type)
                                // this.$set(this.formData,this.formData.project_source_id,res.data.project_source_id)
                                // this.$set(this.formData,this.formData.project_source_name,res.data.project_source)
                                // this.$set(this.formData,this.formData.sign_category_id,res.data.apply_type_id)
                                // this.$set(this.formData,this.formData.sign_category_name,res.data.apply_type)
                                // this.$set(this.formData,this.formData.drug_category,res.data.medical_type)
                                // this.$set(this.formData,this.formData.drug_or_specifications,res.data.dosage_norms)
                                // this.$set(this.formData,this.formData.drug_ingredient,res.data.medical_ingredient)
                                // this.$set(this.formData,this.formData.function_attend,res.data.function_attending)
                                // this.$set(this.formData,this.formData.demand_info_id,val.info_id)
                                // this.$set(this.formData,this.formData.demand_info_name,val.project_name)
                                // this.$set(this.formData,this.formData.project_source_name,this.formData.project_source)

                                if (this.$refs.ruleForm){
                                    this.$refs.ruleForm.clearValidate()
                                }

                                this.isNoDisable = true
                            }else {
                                this.$message.error(res.msg)
                            }
                        }).catch(err=>{

                        })
                    }
                }else{
                    this.formData.demand_info_id = ""
                    this.formData.demand_info_name = ""

                    this.isNoDisable = false
                }

            },
            sourceChange:function (id) {

                if (id){
                    let index = this.projectSourceOptions.findIndex(a=>a.value == id)
                    if (index != -1){
                        let data = this.projectSourceOptions[index];
                        this.formData.project_source_id = data.value + '';
                        this.formData.project_source_name = data.label;

                        this.$set(this.formData,this.formData.project_source_id,data.value + '')
                        this.$set(this.formData,'project_source_name',data.label)
                        this.$set(this.formData,this.formData.project_source_name,data.label)
                    }
                }else{

                    this.$set(this.formData,this.formData.project_source_id,'')
                    this.$set(this.formData,this.formData.project_source_name,'')
                }

            },
            projectChange:function(id){
                if (id){
                    let index = this.projectOptions.findIndex(a=>a.id == id)
                    if (index != -1){
                        let data = this.projectOptions[index];

                        this.formData.project_no = data.project_no
                        this.formData.project_name = data.project_name
                        this.formData.project_category_id = data.project_category_id
                        this.formData.project_category_name = data.project_category_name
                        this.formData.project_source_id = data.project_source_id
                        this.formData.project_source_name = data.project_source_name
                        this.formData.sign_category_id = data.sign_category_id
                        this.formData.sign_category_name = data.sign_category_name

                        this.formData.drug_category = data.drug_category
                        this.formData.drug_or_specifications = data.drug_or_specifications
                        this.formData.drug_ingredient = data.drug_ingredient
                        this.formData.function_attend = data.function_attend

                        this.formData.demand_info_id = data.demand_info_id
                        this.formData.demand_info_name = data.demand_info_name
                        this.formData.project_source_name = data.project_source_name
                        this.formData.project_level_id = data.project_level_id
                        this.formData.project_level_name = data.project_level_name;
                        this.formData.drug_ingredient = data.drug_ingredient
                        this.formData.drug_category = data.drug_category
                        this.formData.function_attend = data.function_attend
                        this.formData.drug_or_specifications = data.drug_or_specifications
                    }
                }else {
                    this.formData.parent_id = "";
                    this.formData.demand_info_id = '';
                    this.formData.demand_info_name = ''
                }
            },
            phasesChange:function (id) {
                if (id){
                    let index = this.phasesOptions.findIndex(a=>a.value == id)
                    if (index != -1){
                        let data = this.phasesOptions[index];
                        this.formData.project_phases_name = data.label;
                        this.formData.project_phases_id = data.value;
                        if (this.formData.project_phases_id == 1){
                            this.formData.parent_id = "";
                        }
                    }
                }else {
                    this.formData.project_phases_name = '';
                    this.formData.project_phases_id = '';
                    this.formData.parent_id = "";
                }

            },
            levelChange:function (id) {
                if (id){
                    let index = this.options.findIndex(a=>a.id == id)
                    if (index != -1){
                        let data = this.options[index];

                        this.$set(this.formData,this.formData.project_level_name,data.dict_val)
                        this.$set(this.formData,'project_level_name',data.dict_val)
                        this.$set(this.formData,this.formData.project_level_id,data.id + '')
                    }
                }else {

                    this.$set(this.formData,this.formData.project_level_name,'')
                    this.$set(this.formData,this.formData.project_level_id,'')
                }

            },
            categoryChange:function (id) {

                if (id){
                    let index = this.projectTypeOptions.findIndex(a=>a.id == id)
                    if (index != -1){
                        let data = this.projectTypeOptions[index];
                        // this.formData.project_category_name = data.dict_val;
                        // this.formData.project_category_id = data.id + '';

                        this.$set(this.formData,this.formData.project_category_name,data.dict_val)
                        this.$set(this.formData,'project_category_name',data.dict_val)
                        this.$set(this.formData,this.formData.project_category_id,data.id + '')
                    }
                }else {

                    this.$set(this.formData,this.formData.project_category_name,'')
                    this.$set(this.formData,this.formData.project_category_id,'')
                }

            },
            signChange:function (id) {
                console.log(id)
                if (id){
                    let index = this.applyTypeOptions.findIndex(a=>a.id == id)
                    if (index != -1){
                        let data = this.applyTypeOptions[index];
                        // this.formData.sign_category_name = data.dict_val;
                        // this.formData.sign_category_id = data.id + '';
                        this.$set(this.formData,this.formData.sign_category_name,data.dict_val)
                        this.$set(this.formData,'sign_category_name',data.dict_val)
                        this.$set(this.formData,this.formData.sign_category_id,data.id + '')
                        console.log(this.formData)
                    }
                } else {
                    this.$set(this.formData,this.formData.sign_category_name,'')
                    this.$set(this.formData,this.formData.sign_category_id,'')

                }

            },
            peopleChange:function (id) {
                if (id){
                    let index = this.peoples.findIndex(a=>a.id == id)
                    if (index != -1){
                        let data = this.peoples[index];


                        this.$set(this.formData,this.formData.project_person_in_charge,data.name)
                        this.$set(this.formData,this.formData.project_person_in_charge_id,data.id + '')
                    }
                }else {

                    this.$set(this.formData,this.formData.project_person_in_charge,'')
                    this.$set(this.formData,this.formData.project_person_in_charge_id,'')
                }

            },
            checkForm:function () {
                let that = this;
                let data = false;
                this.$refs.ruleForm.validate((valid) => {
                    // if (valid) {
                    //     data = that.formData;
                    // }
                    data = valid
                });
                return data;

            }
        }
    }
</script>

<style lang="scss" scoped>
  @import "../../../../../assets/scss/views/projectManagement/list/informationView.scss";
</style>
